var RoomData = require("room");
var EventMgr = require("event_manager");

var TEST_VERSION = 1;

cc.Class({
    extends: cc.Component,

    properties: {
        _peoplemode: 0,         //多少人场
        
        peoplemode: {
            get: function () {
                return this._peoplemode;
            },
            set: function (value) {
                this._peoplemode = value;
            },
        },
        
        ten_on: cc.SpriteFrame,
        ten_off: cc.SpriteFrame,
        twenty_on: cc.SpriteFrame,
        twenty_off: cc.SpriteFrame,
        diamond3: cc.SpriteFrame,
        diamond5: cc.SpriteFrame,
        poker_5: cc.SpriteFrame,
        poker_10: cc.SpriteFrame,
        poker_A: cc.SpriteFrame,
        
        gamechoosemapai_prefab: cc.Prefab,
        
        roundnum: 0,            //多少局
        mapai: 0,               //马牌的值
        temp_mapai: 0,          //记录马牌的临时值
    },

    onLoad: function () {
        var self = this;
        EventMgr.on("socket_createroom", this.onCreateRoom.bind(this));
        //屏蔽底部点击事件
        cc.find('mask', this.node).on(cc.Node.EventType.TOUCH_START, function (event) {});
        
        var toggle_node = cc.find("choosemode_bg/toggle", this.node);
        toggle_node.on(cc.Node.EventType.TOUCH_END, function (event) {
            self.onToggle();
        });

        var choosemode_node = cc.find("choosemode_bg", this.node);
        choosemode_node.scale = 0;
        choosemode_node.runAction(cc.scaleTo(0.3, 1, 1));
    },
    
    onDestroy: function () {
        EventMgr.removeEvent("socket_createroom");
    },
    
    onToggle: function () {
        var toggle_node = cc.find("choosemode_bg/toggle", this.node);
        if (toggle_node.getComponent(cc.Toggle).isChecked) {
            this.temp_mapai = this.mapai;
            this.mapai = 0;
        } else {
            if (this.temp_mapai) {
                this.mapai = this.temp_mapai;
            } else {
                this.mapai = 1;
            }
        }
    },

    //关闭
    onClose: function () {
        var choosepeople_node = cc.find("choosemode_bg", this.node);
        choosepeople_node.runAction(cc.sequence(
            cc.scaleTo(0.3, 0, 0), 
            cc.callFunc(this.exitThisNode, this)
            ));
    },
    
    //删除当前节点
    exitThisNode: function () {
        this.node.destroy();
    },
    
    //确定
    onSure: function () {
        var mapai;
        var havemapai;
        var roundnum;
        var toggle_node = cc.find("choosemode_bg/toggle", this.node);
        if (toggle_node.getComponent(cc.Toggle).isChecked) {
            havemapai = 1;
            if (this.mapai === 1) {
                mapai = 53;
            } else if (this.mapai === 2) {
                mapai = 103;
            } else if (this.mapai === 3) {
                mapai = 143;
            } else {
                mapai = 53;
            }
        } else {
            havemapai = 0;
            this.mapai = 0;
            mapai = 53;
        }
        if (this.roundnum) {
            roundnum = 20;
        } else {
            roundnum = 10;
        }

        var gamedata_node = cc.find("GameData");
        var gamedata_component = gamedata_node.getComponent("Game_Data");
        
        if (TEST_VERSION) {
            var room = new RoomData(havemapai, (this.peoplemode + 3), roundnum, "123456");
            gamedata_component.roomdata = room;
            cc.director.loadScene("game_scene", function (err, scene) {
                if (err) {
                    cc.warn("gamechoosemode_component load game_scene err");
                }
            });
        return;
        }
        var buf = {"data":{"createUserUId":gamedata_component.myuserinfo.uid,"gamesNumber":roundnum,"people":this.peoplemode + 3,"horse":havemapai,"horseBrand":mapai},"pid":1001};
        cc.find("websocket").getComponent("websocket").websocket.send(JSON.stringify(buf));
    },
    
    //创建房间回调
    onCreateRoom: function (message) {
        cc.log(message);
        var ishavema;
        var gamedata_node = cc.find("GameData");
        var gamedata_component = gamedata_node.getComponent("Game_Data");
        if (message.data.horse) {
            ishavema = message.data.horse;
        } else {
            ishavema = 0;
        }
        
        var gamedata_node = cc.find("GameData");
        var gamedata_component = gamedata_node.getComponent("Game_Data");
        var room = new RoomData(ishavema, message.data.people, message.data.gamesNumber, message.data.roomCode);
        gamedata_component.roomdata = room;
        
        cc.director.loadScene("game_scene", function (err, scene) {
            if (err) {
                cc.warn("gamechoosemode_component load game_scene err");
            }
        });
    },
    
    //10局
    onTouchTenRound: function () {
        this.roundnum = 0;
        var tenround_node = cc.find("choosemode_bg/tenround_on", this.node);
        tenround_node.getComponent(cc.Sprite).spriteFrame = this.ten_on;
        var twentyround_node = cc.find("choosemode_bg/twentyround_on", this.node);
        twentyround_node.getComponent(cc.Sprite).spriteFrame = this.twenty_off;
        var diamond_node = cc.find("choosemode_bg/diamondx3", this.node);
        diamond_node.getComponent(cc.Sprite).spriteFrame = this.diamond3;
    },
    
    //20局
    onTouchtwentyRound: function () {
        this.roundnum = 1;
        var tenround_node = cc.find("choosemode_bg/tenround_on", this.node);
        tenround_node.getComponent(cc.Sprite).spriteFrame = this.ten_off;
        var twentyround_node = cc.find("choosemode_bg/twentyround_on", this.node);
        twentyround_node.getComponent(cc.Sprite).spriteFrame = this.twenty_on;
        var diamond_node = cc.find("choosemode_bg/diamondx3", this.node);
        diamond_node.getComponent(cc.Sprite).spriteFrame = this.diamond5;
    },
    
    //选马牌
    onTouchMapai: function () {
        var gamechoosemapai = cc.instantiate(this.gamechoosemapai_prefab);
        gamechoosemapai.getComponent("gamechoosemapai_component").createroom_node = this;
        this.node.addChild(gamechoosemapai);
    },
    
    //设置马牌的值
    onSetMapai: function (value) {
        this.mapai = value;
        this.temp_mapai = value;
        var mapai_node = cc.find("choosemode_bg/poker_103", this.node);
        switch (value) {
            case 1:
                mapai_node.getComponent(cc.Sprite).spriteFrame = this.poker_5;
                break;
            case 2:
                mapai_node.getComponent(cc.Sprite).spriteFrame = this.poker_10;
                break;
            case 3:
                mapai_node.getComponent(cc.Sprite).spriteFrame = this.poker_A;
                break;
        }
    },
});
